package org.example.im.server.handler.impl;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.example.im.common.util.SessionUtil;
import org.springframework.stereotype.Component;

/**
 * @author dingrui
 * @since 2022/2/11
 * @description
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class AuthHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (!SessionUtil.hasLogin(ctx.channel()))
            ctx.channel().close();
        else {
            ctx.pipeline().remove(this);
            super.channelRead(ctx, msg);
        }
    }
}
